//
//  MTSearchFriendResultCell.swift
//  MT
//
//  Created by ooops on 2020/5/11.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory

class MTSearchFriendResultCell: UITableViewCell {
    
    private var data: SearchFriendResultItemStruct?
    
//    var user: JMSGUser! {
//        didSet {
//            switch data?.type {
//            case .singleSign:
//                ItemNameLabel.text = user.signature ?? "暂无签名"
//            case .from:
//                ItemNameLabel.text = "来自XXXXXXX"
//            case .addContacter:
//                ItemNameLabel.isHidden = true
//                exItemNameLabel.isHidden = false
//            default:
//                ItemNameLabel.text = ""
//            }
//        }
//    }
    
    var userModel: MTSearchUserInfoModel? {
        didSet {
            if let m = userModel {
                switch data?.type {
                case .singleSign:
                    ItemNameLabel.text = m.signature ?? "暂无签名"
                case .from:
                    ItemNameLabel.text = "来自XXXXXXX"
                case .addContacter:
                    ItemNameLabel.isHidden = true
                    exItemNameLabel.isHidden = false
                default:
                    ItemNameLabel.text = ""
                }
            }
        }
    }
    
    func exLayout() {
        ItemNameLabel.isHidden = true
        exItemNameLabel.isHidden = false
    }
    
    weak var delegate: MTChatSingleDetailCellDelegate?
    
    lazy var actionItemNameLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(14)
        l.textColor = UIColor.text.x1d1a1a
        return l
    }()
    
    lazy var ItemNameLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(12)
        l.textColor = UIColor.text.x9b9b9b
        return l
    }()
    
    lazy var exItemNameLabel: UILabel = {
        let l = UILabel()
        l.isHidden = true
        l.textAlignment = .center
        l.font = PingFangSCSemiboldFont(16)
        l.textColor = UIColor(hex: 0x435278)
        return l
    }()
    
    lazy var arrowIconImageView: UIImageView = {
        let imageV = UIImageView()
        imageV.image = UIImage(named: "icon_arrow_right_gary")
        imageV.isHidden = true
        return imageV
    }()
    
    lazy var lineView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(hex: 0xE1E1E1)
        return v
    }()
    
    func bindData(data: SearchFriendResultItemStruct) {
        arrowIconImageView.isHidden = !data.isShowArrow
        if data.type == .addContacter {
            ItemNameLabel.isHidden = true
            exItemNameLabel.isHidden = false
            exItemNameLabel.text = data.itemName
        } else {
            ItemNameLabel.isHidden = false
            exItemNameLabel.isHidden = true
            actionItemNameLabel.text = data.itemName
        }
        self.data = data
    }
    
    class func reuseIdentity() -> String {
        return "MTSearchFriendResultCellID"
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        makeConstraint()
        selectionStyle = .none
    }
    
    func showLine(long: Bool) {
        if long {
            lineView.snp.updateConstraints { (make) in
                make.left.equalToSuperview().offset(20)
            }
        } else {
            lineView.snp.updateConstraints { (make) in
                make.left.equalToSuperview().offset(0)
            }
        }
    }
    
    private func setupUI() {
        contentView.addSubview(actionItemNameLabel)
        contentView.addSubview(ItemNameLabel)
        contentView.addSubview(arrowIconImageView)
        contentView.addSubview(lineView)
        
        contentView.addSubview(exItemNameLabel)
    }
    
    private func makeConstraint() {
        actionItemNameLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(10)
        }
        
        ItemNameLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(actionItemNameLabel)
            make.left.equalToSuperview().offset(80)
        }
        
        exItemNameLabel.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        arrowIconImageView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-10)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.right.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(0)
            make.height.equalTo(1)
        }
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
